package mango;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;


import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
public class MongoDB {
	
	// 测试连接
	public static void main(String args[]) {
		// 连接到MongoDB服务
		MongoClient mongoClient = new MongoClient("172.16.63.145", 27017);
		// 测试输出数据库名称
		List<String> dbNames = mongoClient.getDatabaseNames();
		
		System.out.println("*************************Connect to database successfully*************************");
		
		 while(true)
	      {
	      System.out.println("**************请选择功能：1.增加数据2.删除数据3.查找所有数据4.按条件查找数据5.修改数据6.创建数据表**************");
	      Scanner input=new Scanner(System.in);
	      int n=input.nextInt();//输入选择
	      switch(n)
	      {
	      	case 1://增加数据
	      		insert();
	      		break;
	      	case 2:
	      		delete();
	      		break;
	      	case 3:
	      		find();
	      		break;
	      	case 4:
	      		search();
	      	case 5:
	      		update();
	      		break;
	      	case 6:
	      		create();
	      		break;
	      }
	      }
		
	}

		// 连接数据库
		public static MongoDatabase connect() {
			// 连接到 mongodb服务
			MongoClient mongoClient = new MongoClient("172.16.63.145", 27017);
			// 连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("testexample");
			System.out.println("Connect to database successfully");
			return mongoDatabase;
		}
		
		// 创建集合
		public static void create() {
			// 连接到 mongodb服务，并连接到数据库
			MongoDB mdb = new MongoDB();
			MongoDatabase mongoDatabase = mdb.connect();
			// 创建集合
			mongoDatabase.createCollection("java");
			System.out.println("集合java创建成功");
		}

		// 获取集合
		public static MongoCollection<org.bson.Document> getCol() {
			// 连接到 mongodb服务，并连接到数据库
			MongoDB mdb = new MongoDB();
			MongoDatabase mongoDatabase = mdb.connect();
			// 获取集合
			MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("java");
			System.out.println("集合 java 选择成功");
			return collection;
		}
		
		// 插入文档
		public static void insert() {
			// 连接到 mongodb服务，连接到数据库，连接集合
			MongoDB mdb = new MongoDB();
			MongoCollection<org.bson.Document> collection = mdb.getCol();
			// 创建文档
			Document document = new Document("name","李钰萍")
                    .append("sex", "女")
                    .append("age", 20);

			// 插入文档
			collection.insertOne((org.bson.Document) document);
			System.out.println("文档插入成功");
		}

		// 检索所有文档
		public static void find() {
			MongoDB mdb = new MongoDB();
			FindIterable<Document> findIterable = mdb.getCol().find();
			MongoCursor<Document> mongoCursor = findIterable.iterator();
			while (mongoCursor.hasNext()) {
				System.out.println(mongoCursor.next());
			}
		}
		
		//指定查询过滤器查询
		public static void search(){
			MongoDB mdb = new MongoDB();
			Bson filter = Filters.eq("name", "张三");
			FindIterable<Document> findIterable = mdb.getCol().find(filter);
			MongoCursor<Document> mongoCursor = findIterable.iterator();
			while (mongoCursor.hasNext()) {
				System.out.println(mongoCursor.next());
			}
		}
		// 更新文档
		public static  void update() {
			MongoDB mdb = new MongoDB();
			// 更新文档 将文档中likes=100的文档修改为likes=200
			mdb.getCol().updateMany(Filters.eq("name", "张三"), new Document("$set", new Document("age", 50)));
			// 检索查看结果
			FindIterable<Document> findIterable = mdb.getCol().find();
			MongoCursor<Document> mongoCursor = findIterable.iterator();
			while (mongoCursor.hasNext()) {
				System.out.println(mongoCursor.next());
			}
		}

		// 删除文档
		public  static void delete() {
			MongoDB mdb = new MongoDB();
			// 删除符合条件的第一个文档
			mdb.getCol().deleteOne(Filters.eq("age",50));
			// 删除所有符合条件的文档
			
			// 检索查看结果
			FindIterable<Document> findIterable = mdb.getCol().find();
			MongoCursor<Document> mongoCursor = findIterable.iterator();
			while (mongoCursor.hasNext()) {
				System.out.println(mongoCursor.next());
			}
		}


		
			
}
